package com.huangliusong;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;

/**
 * @author huangliusong
 * @since 2019/1/26
 * {@link Watcher}
 */
public class ZKNodeOperator  implements Watcher {
    private ZooKeeper zooKeeper=null;

    public static final String zkServerPath="192.168.1.101:2181";
    public static final Integer timeout=5000;

    public ZKNodeOperator(){

    }

    public static void main(String[] args) throws KeeperException, InterruptedException {
        ZKNodeOperator zkServer=new ZKNodeOperator(zkServerPath);
        zkServer.createZKNode("/testnode","testnode".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE);
       // Stat stat=zkServer.getZooKeeper().setData("/testnode","huangliusong".getBytes(),0);
        //System.out.println(stat);


       //zkServer.getZooKeeper().delete("/testnode",1);
       // Object ctx=new Object();
       // zkServer.getZooKeeper().delete("/testnode",0,new DeleteCallBack(),ctx);

    }

    public  ZKNodeOperator(String connectionStr){
        try {
            zooKeeper=new ZooKeeper(connectionStr,timeout,new ZKNodeOperator());
        } catch (IOException e) {
            e.printStackTrace();
            if (zooKeeper!=null){
                try {
                    zooKeeper.close();
                } catch  (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public void createZKNode(String path, byte[] data, List<ACL> acls){
        String result = "";
        try {
            /**
             * 同步或者异步创建节点，都不支持子节点的递归创建，异步有一个callback函数
             * 参数：
             * path：创建的路径
             * data：存储的数据的byte[]
             * acl：控制权限策略
             * 			Ids.OPEN_ACL_UNSAFE --> world:anyone:cdrwa
             * 			    CREATOR_ALL_ACL --> auth:user:password:cdrwa
             * createMode：节点类型, 是一个枚举
             * 			PERSISTENT：持久节点
             * 			PERSISTENT_SEQUENTIAL：持久顺序节点
             * 			EPHEMERAL：临时节点
             * 			EPHEMERAL_SEQUENTIAL：临时顺序节点
             */

            //同步 返回创建节点的名字
            //result = zooKeeper.create(path, data, acls, CreateMode.EPHEMERAL);

            String ctx = "{'create':'success'}";
            zooKeeper.create(path, data, acls, CreateMode.PERSISTENT, new AsyncCallback.StringCallback(){

                public void processResult(int rc, String path, Object ctx, String name) {
                    System.err.println("rc：" + rc);
                    System.err.println("name：" + name);
                    System.err.println("创建节点：" + path);
                    System.err.println((String)ctx);
                }
            }, ctx);


            System.out.println("创建节点：\t" + result + "\t成功...");
            new Thread().sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ZooKeeper getZooKeeper(){
        return zooKeeper;
    }

    public void setZooKeeper(){
        this.zooKeeper=zooKeeper;
    }
    public void process(WatchedEvent watchedEvent) {
        System.out.print("watchedEvent=>");
        System.out.println(watchedEvent);
    }
}
